<template>
    <el-row class="login-wrapper">
        <el-col :sm="16" :xs="24" class="login-content">
            <h3>登录</h3>
            <el-form>
                <el-form-item lable="用户名">
                    <el-input placeholder="邮箱/手机号/用户名" v-model="user" required class="input" @focus="toggleFooter" @blur="toggleFooter"></el-input>
                </el-form-item>
                <el-form-item lable="用户名">
                    <el-input type="password" placeholder="密码" v-model="pwd" required class="input" @focus="toggleFooter"  @blur="toggleFooter"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button @click="login" type="primary" class="login-btn">登录</el-button>
                    <router-link to="/register"><el-button type="warning" class="register-btn">注册</el-button></router-link>
                </el-form-item>
            </el-form>
            <router-link to="/restpwd"><el-button style="text-align:left; margin-top: -20px" type="text">忘记密码 ？</el-button></router-link>
        </el-col>
    </el-row>
</template>
<script>
import { login } from '@/utils/api'
import { isMobile } from '@/utils/fetch'

export default {
  name: 'Login',
  data () {
    return {
      user: '',
      pwd: ''
    }
  },
  methods: {
    async login () {
      let res = await login(this.user, this.pwd)
      if (res.error === '10') {
        this.$notify({
          title: '登录成功',
          type: 'success'
        })
        this.$store.commit('setUserInfo', res.result)
        if (isMobile()) {
          this.$router.push('/coding')
        } else {
          this.$router.push('/user/profile')
        }
      } else {
        this.$notify({
          title: '登录错误',
          type: 'error',
          message: res.result
        })
      }
    },
    toggleFooter () {
        this.$emit('toggleFooter')
    }
  }
}
</script>
<style lang="less">
.login-wrapper {
    background: #f7f7f7;
    padding-top: 60px;
    height: 100%;
    display: flex;
}

.login-content {
    background: #fff;
    height: calc(~'100vh - 260px');
    display: flex;
    align-items: center;
    flex-direction: column;
    padding-top: 60px;
    margin: 0 auto;
    &>* {
        width: 70%;
        max-width: 280px;
    }
    h3 {
        font-weight: normal;
        margin-bottom: 30px;
    }
    .input input, .login-btn, .register-btn {
        border-radius: 0;
    }
    .login-btn {
        float: left;
        width: 55%;
    }
    .register-btn {
        float: right;
        width: 35%;
    }
}
@media only screen and (max-width: 768px) {
    .login-wrapper {
        padding-top: 0;
    }
    .login-wrapper {
        text-align: center;
    }
}
</style>
